﻿@inject IMenuProvider Menu
@inject SubstrateOptions Options 
@{
    var menuName = (ViewData["NavbarName"] as string) ?? MenuNameDefaults.MainNavbar;
    var suppressUserMenu = ViewData.TryGetValue("SuppressUserMenu", out object? _suppressUserMenu) && _suppressUserMenu is bool __suppressUserMenu && __suppressUserMenu;
    var suppressUserSubmenu = ViewData.TryGetValue("SuppressUserSubmenu", out object? _suppressUserSubmenu) && _suppressUserSubmenu is bool __suppressUserSubmenu && __suppressUserSubmenu;
    var extraMenu = ViewData["ExtraMenu"] as string;
}
<ul class="navbar-nav mr-auto">
    @foreach (var menu in Menu.Find(menuName))
    {
        if (!menu.Satisfy(ViewContext)) { continue; }
        if (menu is ISubmenu submenu)
        {
    <li class="nav-item dropdown" data-order="@menu.Priority">
        <a class="nav-link dropdown-toggle @menu.Active(ViewContext)" href="#" id="@submenu.Id" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            <i class="@menu.Icon"></i> @menu.Title
            @foreach (var badge in menu.Badges)
            {
            <span class="badge badge-@badge.Item2" id="num-alerts-@badge.Item1"></span>
            }
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdown">
            @foreach (var subentry in submenu)
            {
                if (!subentry.Satisfy(ViewContext)) { continue; }
            <a class="dropdown-item" href="@subentry.GetLink(Url, ViewContext)" id="@subentry.Id" data-order="@subentry.Priority">
                <i class="@subentry.Icon fa-fw"></i> @subentry.Title
                @foreach (var badge in subentry.Badges)
                {
                <span class="badge badge-@badge.Item2" id="num-alerts-@badge.Item1-sub"></span>
                }
            </a>
            }
        </div>
    </li>
        }
        else
        {
            var entry = (IMenuEntry)menu;
    <li class="nav-item" data-order="@menu.Priority">
        <a class="nav-link @menu.Active(ViewContext)" href="@entry.GetLink(Url, ViewContext)" id="@entry.Id">
            <i class="@entry.Icon"></i> @entry.Title
            @foreach (var badge in menu.Badges)
            {
            <span class="badge badge-@badge.Item2" id="num-alerts-@badge.Item1"></span>
            }
        </a>
    </li>
        }
    }
</ul>

@if (!string.IsNullOrWhiteSpace(extraMenu))
{
    <partial name="@extraMenu" />
}

<ul class="navbar-nav">
    <li class="ajax-loader-container">
        <div class="spinner-border spinner-border-sm ajax-loader" style="display: none;" role="status">
            <span class="sr-only">Loading...</span>
        </div>
    </li>
    <li class="nav-item dropdown" asp-show-if="!suppressUserMenu && User.IsSignedIn()">
        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            <i class="fas fa-user"></i> @User.GetUserName()
        </a>
        <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown">
            <a class="dropdown-item disabled" href="#">@User.GetNickName()</a>
            <a class="dropdown-item d-none" href="#" id="notify_disable">
                <i class="fas fa-bell-slash fa-fw"></i> Disable Notifications
            </a>
            <a class="dropdown-item d-none" href="#" id="notify_enable">
                <i class="fas fa-bell fa-fw"></i> Enable Notifications
            </a>
            <a asp-viewdata-key="RefreshSpan" class="dropdown-item" href="#" id="refresh-navitem">
                <i class="fas fa-sync-alt fa-fw"></i>
                <span id="refresh-toggle">
                    Disable Refresh
                </span>
                <span class="small text-muted">(@ViewData["RefreshSpan"]s)</span>
            </a>
        @foreach (var subentry in Menu.UserDropdown())
        {
            if (suppressUserSubmenu) { break; }
            if (!subentry.Satisfy(ViewContext)) { continue; }
            if (subentry.GetMetadata("ExcludeMenuNameAt") is string excludeAt && menuName == excludeAt) { continue; }
            <a class="dropdown-item" href="@subentry.GetLink(Url, ViewContext)" asp-meet-policy="@(subentry.GetMetadata("RequiredPolicy") as string)" id="@subentry.Id" data-order="@subentry.Priority">
                <i class="@subentry.Icon fa-fw"></i> @subentry.Title
                @foreach (var badge in subentry.Badges)
                {
                    <span class="badge badge-@badge.Item2" id="num-alerts-@badge.Item1-sub"></span>
                }
            </a>
        }
            <form asp-route="@(Options.LogoutRouteName)" method="post" id="logoutForm">
                <button type="submit" class="dropdown-item">
                    <i class="fas fa-sign-out-alt fa-fw"></i> Logout
                </button>
            </form>
        </div>
    </li>
    <li class="nav-item" asp-show-if="!suppressUserMenu && !User.IsSignedIn()">
        <a class="nav-link" role="button" data-toggle="ajaxWindow" data-target="login" asp-route="@(Options.LoginRouteName)" asp-route-returnUrl="@ViewData["RefreshUrl2"]">
            <i class="fas fa-sign-in-alt fa-fw"></i> Login
        </a>
    </li>
</ul>
